package org.teamh.connection.json.command;

import java.util.LinkedList;
import java.util.Queue;


public class JsonCommandObject {

	private String action;
	private Queue<JsonCommandEntry> entries;
	
	public JsonCommandObject(String action) {
		this.action = action;
		this.entries = new LinkedList<JsonCommandEntry>();
	}
	
	public void addEntry(String key, String value) {
		entries.add(new JsonCommandEntry(key, value));
	}
	
	public String jsonToString() {
		String msg = "";
		if (entries.isEmpty()) {
			msg += "{\"@action\":\"" + action + "\"}";
			return msg;
		} else {
			msg += "{\"@action\":\"" + action + "\",\"properties\":{";
			while (!entries.isEmpty()) {
				msg += "\"entry\":{\"key\":\"" + entries.peek().getKey() + "\",\"value\":\"" + entries.poll().getValue() + "\"}";
				if (!entries.isEmpty()) {
					msg += ",";
				}
			}
			msg += "}}";
			return msg;
		}		
	}
	
	public String getAction() {
		return action;
	}
	
	public Queue<JsonCommandEntry> getEntries() {
		return entries;
	}
	
	public void setAction(String action) {
		this.action = action;
	}
	
	public void setEntries(Queue<JsonCommandEntry> entries) {
		this.entries = entries;
	}
	
}
